﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TByte.XnaHelper;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TByte.XnaHelper.Input;

namespace TByte.XnaHelper.GameObjects
{
    /// <summary>
    /// This game object represents a clickable button.
    /// It will raise the OnClick event when the button is hit.
    /// </summary>
    public class ButtonGameObject : GameObject
    {
        #region Fields
        SpriteSheet sheet;
        bool hover;
        public event Action OnClick;
        string text;
        SpriteFont font;
        Rectangle rect;
        Color fontColor;
        #endregion

        #region Methods
        /// <summary>
        /// Update the button.
        /// </summary>
        public override void Update()
        {
            rect = new Rectangle((int)Position.X, (int)Position.Y, Width, Height);

            if (rect.Contains(InputSystem.MouseLocation))
            {
                hover = true;
            }
            else
            {
                hover = false;
            }
            base.Update();
        }
        /// <summary>
        /// Draw the button
        /// </summary>
        /// <param name="spriteBatch">An opened spriteBatch (Call begin before passing)</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (sheet != null)
                sheet.Draw(spriteBatch, Position, hover ? 1 : 0);
            else if (hover)
                spriteBatch.Draw(Manager.dummyTexture, rect, Color.Black * 0.5f);
            spriteBatch.DrawString(font, text, Position + (new Vector2(rect.Width, rect.Height) - font.MeasureString(text)) / 2, fontColor);
            base.Draw(spriteBatch);
        }
            
        public override void OnMouseDown(TByte.XnaHelper.Input.MouseEventArgs e)
        {
            if (e.Button == MouseButton.Left)
            {
                if (hover && OnClick != null)
                {
                    OnClick();
                    hover = false;
                }
            }
            base.OnMouseDown(e);
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Create a new Button
        /// </summary>
        /// <param name="sheet">The button spriteSheet. (Has to contain at least 2 sprites. 0 for regular and 1 for hover)</param>
        /// <param name="text">The text that the button should display.</param>
        /// <param name="fontColor">The font Color</param>
        /// <param name="font">A SpriteFont for the button text</param>
        /// <param name="name">A gameobject string identifier.</param>
        public ButtonGameObject(SpriteSheet sheet, string text, Color fontColor, SpriteFont font, string name)
            : base(name)
        {
            this.sheet = sheet;
            this.text = text;
            this.fontColor = fontColor;
            this.font = font;
        }
        #endregion

        #region Properties
        public int Height { get { return sheet == null ? (int)font.MeasureString(text).Y + 10 : sheet.Height(0); } }
        public int Width { get { return sheet == null ? (int)font.MeasureString(text).X + 20: sheet.Width(0); } }
        #endregion
    }
}
